package com.woniu.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.order.client.OrderQueryClient;
import com.woniu.result.Result;
import com.woniu.user.client.AddressManageClient;
import com.woniu.user.client.param.InsertAddressParam;
import com.woniu.user.client.param.UpdateAddress;
import com.woniu.user.service.AddressService;
import com.woniu.user.service.impl.AddressServiceImpl;
import com.woniu.user.service.param.InsertAddressServiceParam;
import com.woniu.user.service.param.UpdateServiceAddress;
import com.woniu.user.service.param.UpdateUserInfoServiceParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class AddressManageApiImpl implements AddressManageClient {

    @Resource
    private AddressService addressService;

    //添加地址
    @Override
    public Result insertAddress(InsertAddressParam insertAddressParam) {
        log.info("检查添加地址参数：{}", insertAddressParam);
        InsertAddressServiceParam insertAddressServiceParam = BeanUtil.copyProperties(insertAddressParam, InsertAddressServiceParam.class);
        boolean b = addressService.insertAddress(insertAddressServiceParam);
        if (b) return Result.getInstance().setMsg("添加成功");
        return Result.getInstance().setMsg("添加失败");
    }

    //设为默认地址
    @Override
    public Result defultAddress(Integer addressId) {

        log.info("检查地址的ID：{}",addressId);
        boolean b = addressService.defultAddress(addressId);
        if (b) return Result.getInstance().setMsg("设定成功");
        return Result.getInstance().setMsg("设定失败");
    }

    //修改地址
    @Override
    public Result updateAddress(UpdateAddress updateAddress){
        log.info("检查修改的参数：{}",updateAddress);
        UpdateServiceAddress updateServiceAddress = BeanUtil.copyProperties(updateAddress, UpdateServiceAddress.class);
        boolean b = addressService.updateAddress(updateServiceAddress);
        if (b) return Result.getInstance().setMsg("修改成功");
        return Result.getInstance().setMsg("修改失败");
    }

    //删除地址
    @Override
    public Result deleteAddress(Integer addressId) {
        log.info("检查地址的ID：{}",addressId);
        boolean b = addressService.deleteAddress(addressId);
        if (b) return Result.getInstance().setMsg("删除成功");
        return Result.getInstance().setMsg("删除失败");
    }
}
